﻿Public Class ConfigINI
    ''' <summary>
    ''' funciones de la api windows para leer ficheros ini
    ''' </summary>
    ''' <param name="lpApp"></param>
    ''' <param name="lpKeyName"></param>
    ''' <param name="ByVallpDefault"></param>
    ''' <param name="lpReturnString"></param>
    ''' <param name="nSize"></param>
    ''' <param name="lpFilename"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Declare Ansi Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApp As String, ByVal lpKeyName As String, ByVal ByVallpDefault As String, ByVal lpReturnString As System.Text.StringBuilder, ByVal nSize As Integer, ByVal lpFilename As String) As Integer
    Private Declare Ansi Function WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Integer

    Private Declare Ansi Function GetPrivateProfileInt Lib "kernel32.dll" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Integer, ByVal lpFileName As String) As Integer

    Private Declare Ansi Function FlushPrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As Integer, ByVal lpKeyName As Integer, ByVal lpString As Integer, ByVal lpFileName As String) As Integer

    Private strFilename As String
    Public ReadOnly Property FileName As String
        Get
            Return strFilename
        End Get
    End Property

    Public Sub New(ByVal Filename As String)
        Me.strFilename = Filename
    End Sub

    'Función para leer cadena de texto (string) de fichero INI
    Public Function GetString(ByVal Section As String, _
     ByVal Key As String, ByVal [Default] As String) As String
        Dim intCharCount As Integer
        Dim objResult As New System.Text.StringBuilder(256)

        intCharCount = GetPrivateProfileString(Section, Key, _
           [Default], objResult, objResult.Capacity, strFilename)
        If intCharCount > 0 Then
            GetString = Left(objResult.ToString, intCharCount)
        Else
            GetString = ""
        End If
    End Function

    'Función para leer un valor numérico del fichero INI
    Public Function GetInteger(ByVal Section As String, _
      ByVal Key As String, ByVal [Default] As Integer) As Integer
        Return GetPrivateProfileInt(Section, Key, _
           [Default], strFilename)
    End Function

    'Función para leer un valor booleano de fichero INI
    Public Function GetBoolean(ByVal Section As String, _
      ByVal Key As String, ByVal [Default] As Boolean) As Boolean
        Return (GetPrivateProfileInt(Section, Key, _
           CInt([Default]), strFilename) = 1)
    End Function

    'Función para escribir valor de cadena (string) en fichero INI
    Public Sub WriteString(ByVal Section As String, _
      ByVal Key As String, ByVal Value As String)
        WritePrivateProfileString(Section, Key, Value, strFilename)
        Flush()
    End Sub

    'Función para escribir valor numérico en fichero INI
    Public Sub WriteInteger(ByVal Section As String, _
      ByVal Key As String, ByVal Value As Integer)
        ' Writes an integer to your INI file
        WriteString(Section, Key, CStr(Value))
        Flush()
    End Sub

    'Función para escribir valor booleano en fichero INI
    Public Sub WriteBoolean(ByVal Section As String, _
    ByVal Key As String, ByVal Value As Boolean)
        ' Writes a boolean to your INI file
        WriteString(Section, Key, CStr(Math.Abs(CInt(Value))))
        Flush()
    End Sub

    'Guarda los cambios de la caché en fichero INI
    Private Sub Flush()
        FlushPrivateProfileString(0, 0, 0, strFilename)
    End Sub
End Class

